<template>
    <div>
        <van-sticky>
            <van-nav-bar title="商品列表" left-text="返回" left-arrow @click-left="() => { $router.push('/goods') }" />
        </van-sticky>
        <van-skeleton v-if="pd">
            <template #template>
                <van-skeleton-image style="width: 100%;"/>
            </template>
        </van-skeleton>
        <swiper :images="xq.pics" />
        <div v-html="xq.goods_introduce"></div>
        <van-action-bar>
            <van-action-bar-icon icon="chat-o" text="客服" />
            <van-action-bar-icon icon="cart-o" text="购物车" />
            <van-action-bar-icon icon="shop-o" text="店铺" />
            <van-action-bar-button type="danger" text="加入购物车" @click="onClickButton" />
        </van-action-bar>

    </div>
</template>

<script setup>
import { showToast } from 'vant'
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { detailApi } from '../request/api'
import { useCounterStore } from '../stores/counter'
const store = useCounterStore()
const route = useRoute()
const router = useRouter()
let pd = ref(true)
const xq = ref({})
detailApi(route.params.goods_id).then(res => {
    setTimeout(() => {
        xq.value = res.data.message
        pd.value = false
    }, 500)
})
const onClickButton = () => {
    let token = localStorage.getItem('token')
    if (!token) {
        showToast({
            type: 'fail',
            message: '快去登录',
            onClose() {
                router.push('/login')
            }
        })
    } else {
        store.add(xq.value, route.params.goods_id)
        showToast({
            type: 'success',
            message: '加购成功',
            onClose() {
                router.push('/home/cart')
            }
        })
    }
}

</script>

<style lang="scss" scoped></style>
